Skip to content

Fix parsing xpath that has mult without surrounding spaces#321

Closed
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:xpath_mult_fix
Closed

Fix parsing xpath that has mult without surrounding spaces#321
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:xpath_mult_fix

Conversation

@tompng
Copy link
Copy Markdown
Member

@tompng tompng commented Jun 3, 2026

Fix PathExpr's implementation to match its source code comments. Fixes parsing xpath such as /a[(1+2)*3].

REXML::Parsers::XPathParser.new.parse("(1+2) * 3")
#=> [:mult, [:plus, [:literal, 1], [:literal, 2]], [:literal, 3]]
REXML::Parsers::XPathParser.new.parse("(1+2)*3")
#=> REXML::ParseException (before)
#=> [:mult, [:plus, [:literal, 1], [:literal, 2]], [:literal, 3]] (after)

REXML::Parsers::XPathParser.new.parse("a[(1+2)*3]")
#=> [:child, :qname, "", "a", :predicate, [:plus, [:literal, 1], [:literal, 2], :child, :any]] (before)
#=> [:child, :qname, "", "a", :predicate, [:mult, [:plus, [:literal, 1], [:literal, 2]], [:literal, 3]]] (after)

Source code comment:

#| LocationPath
#| FilterExpr ('/' | '//') RelativeLocationPath 

Actual implementation was:

#| LocationPath
#| FilterExpr ('/' | '//') RelativeLocationPath
#| FilterExpr ('/' | '//') RelativeLocationPath LocationPath

LocationPath was eating the mult operator * as child axis.

Fix PathExpr's implementation to match its source code comments.
Fixes parsing xpath such as `/a[(1+2)*3]`.
Copilot AI review requested due to automatic review settings June 3, 2026 14:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates the XPath parser to better handle multiplication (*) and whitespace/adjacent-asterisk cases, and adds regression tests to validate equivalent parsing with/without spaces.

Changes:

  • Added parser tests covering multiplication and tricky * spacing/adjacent-token cases.
  • Adjusted PathExpr parsing flow to treat unconsumed input as a LocationPath and handle /-prefixed continuations.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
test/parser/test_xpath.rb Adds regression tests for * operator parsing with varying whitespace/adjacent * tokens.
lib/rexml/parsers/xpathparser.rb Tweaks PathExpr control flow to route unconsumed input into LocationPath, impacting how adjacent * tokens are interpreted.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +594 to +596
if rest == path
rest = LocationPath(rest, n)
else
Comment thread test/parser/test_xpath.rb
def test_mult_and_spaces
parser = REXML::Parsers::XPathParser.new
assert_equal(parser.parse("1 * 2 * 3"), parser.parse("1*2*3"))
assert_equal(parser.parse("a[( ( 1 + 2 ) * 3 + 4 * ( 5 + 6 ) ) * 7 < 8]"), parser.parse("a[((1+2)*3+4*(5+6))*7<8]"))
@tompng
Copy link
Copy Markdown
Member Author

tompng commented Jun 3, 2026

Closing because #322 is better

@tompng tompng closed this Jun 3, 2026
@tompng tompng deleted the xpath_mult_fix branch June 3, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants